package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.StockMaster;
import com.sevattal.service.leaves.entity.vo.StockMasterVo;
import com.sevattal.service.leaves.mapper.StockMasterMapper;
import com.sevattal.service.leaves.service.StockMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class StockMasterServiceImpl extends ServiceImpl<StockMasterMapper, StockMaster> implements StockMasterService {

    @Override
    public boolean createStockMaster(StockMaster stockMaster) {
        if (stockMaster.getFWhid() == null || StringUtils.isEmpty(stockMaster.getFWhid())
        || stockMaster.getFMid() == null || StringUtils.isEmpty(stockMaster.getFMid())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<StockMaster> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("f_whId",stockMaster.getFWhid()).eq("f_mId",stockMaster.getFMid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.insert(stockMaster);
        return result == 1;
    }

    @Override
    public boolean deleteStockMaster(String whid,String mid) {
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_whId",whid).eq("f_mId",mid);
        int result = baseMapper.delete(queryWrapper);
        return result == 1;
    }

    @Override
    public boolean updateStockMaster(StockMaster stockMaster) {
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_whId",stockMaster.getFWhid()).eq("f_mId",stockMaster.getFMid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count == 0){
           throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.update(stockMaster,queryWrapper);
        return result == 1;
    }

    @Override
    public List<StockMasterVo> getStockMasterList(StockMasterVo stockMaster) {
        QueryWrapper<StockMaster> queryWrapper= new QueryWrapper<>();
        queryWrapper.orderByAsc("sm.f_whId","sm.f_mId");
        queryWrapper.eq("sm.is_disabled",0);
        queryWrapper.eq("sm.is_deleted",0);
        if (stockMaster.getFWhid() != null && !StringUtils.isEmpty(stockMaster.getFWhid())){
            queryWrapper.eq("sm.f_whId",stockMaster.getFWhid());
        }
        if (stockMaster.getFMid() != null && !StringUtils.isEmpty(stockMaster.getFMid())){
            queryWrapper.like("sm.f_mId",stockMaster.getFMid());
        }
        if (stockMaster.getFMname() != null && !StringUtils.isEmpty(stockMaster.getFMname())){
            queryWrapper.like("sm.f_mName",stockMaster.getFMname());
        }
        if (stockMaster.getFMkind() != null ){
            queryWrapper.eq("m.f_mKind",stockMaster.getFMkind());
        }

        List<StockMasterVo>  stockMasterVoList= baseMapper.getStockMasterList(queryWrapper);
        return stockMasterVoList;
    }
}
